package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.StockMaterialAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.entity.StockMaterial;
import com.isunland.managesystem.entity.StockMaterialOriginal;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * 对象功能:  库存物料查询列表
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/6/28 10:08
 */
public class StockMaterialListFragment extends BaseListFragment {

  private int REQUEST_QUERY = 0;
  private String storagePlaceId;
  private String storagePlaceName;
  private String beginmindealDate;
  private String endmindealDate;
  private ArrayList<StockMaterial> mlist;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom("库存物料列表");
    storagePlaceId = "";
    storagePlaceName = "";
    beginmindealDate = MyDateUtil.getFirstDay();
    endmindealDate = MyDateUtil.format(new Date(), MyDateUtil.TYPE_DATE);
    mlist = new ArrayList<>();
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mListview.setDividerHeight(0);//去掉分隔线
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_query, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_query:
        ExtraValueContent queryContent = new ExtraValueContent();
        queryContent.setExtCode1(storagePlaceId);
        queryContent.setExtName1(storagePlaceName);
        queryContent.setExtName2(beginmindealDate);
        queryContent.setExtName3(endmindealDate);
        Intent query = new Intent(getActivity(), StockMaterialQueryActivity.class);
        query.putExtra(StockMaterialQueryFragment.EXTRA_VALUE, queryContent);
        startActivityForResult(query, REQUEST_QUERY);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    return ApiConst.URL_STOCK_MATERIAL;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    //默认统计项：仓库、物料名称、生产厂家、生产批号
    params.put("storagePlaceId", storagePlaceId);
    params.put("beginmindealDate", beginmindealDate);
    params.put("endmindealDate", endmindealDate);
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    StockMaterialOriginal original = new Gson().fromJson(json, StockMaterialOriginal.class);
    if (Base.SUCCESS == original.getResult() && original.getRows() != null) {
      mlist.clear();
      mlist.addAll(original.getRows());
      StockMaterialAdapter madapter = new StockMaterialAdapter(getActivity(), mlist);
      setListAdapter(madapter);
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == REQUEST_QUERY) {
      ExtraValueContent queryContent =
          (ExtraValueContent) data.getSerializableExtra(StockMaterialQueryFragment.EXTRA_VALUE);
      storagePlaceId = queryContent.getExtCode1();
      storagePlaceName = queryContent.getExtName1();
      beginmindealDate = queryContent.getExtName2();
      endmindealDate = queryContent.getExtName3();
      volleyPost();
    }

    super.onActivityResult(requestCode, resultCode, data);
  }
}
